﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Panels
{
    public partial class StatusWindow : ToolWindow, Library.Objects.IStatusLog
    {
        private bool _isSave = false;
        private string _logfile = null;

        public bool IsSave
        {
            get { return _isSave; }
        }

        public StatusWindow()
            : base()
        {
            InitializeComponent();
        }

        public void AddLog(string str)
        {
            if (richTextBox_Status.InvokeRequired)
                richTextBox_Status.BeginInvoke((MethodInvoker)delegate
                {
                    LogNew(str);
                });
            else
            {
                LogNew(str);
            }
        }

        public void AddWarning(string str)
        {
            if (richTextBox_Status.InvokeRequired)
                richTextBox_Status.BeginInvoke((MethodInvoker)delegate
                {
                    LogWarning(str);
                });
            else
                LogWarning(str);
        }

        public void AddError(string str)
        {
            if (richTextBox_Status.InvokeRequired)
                richTextBox_Status.BeginInvoke((MethodInvoker)delegate
                {
                    LogError(str);
                });
            else
            {
                LogError(str);
            }
        }

        public void AddError(string str, Exception ex)
        {
            if (richTextBox_Status.InvokeRequired)
                richTextBox_Status.BeginInvoke((MethodInvoker)delegate
                {
                    LogError(str, ex);
                });
            else
            {
                LogError(str, ex);
            }
        }

        public void Save()
        {
            //if (!_isSave)
            //{
            //    if (!System.IO.Directory.Exists(Nizm0.QOT.Library.Settings.Instance.ProgramLocalization + "\\Logs\\")) System.IO.Directory.CreateDirectory(Nizm0.QOT.Library.Settings.Instance.ProgramLocalization + "\\Logs\\");
            //    int countfiles = System.IO.Directory.GetFiles(Nizm0.QOT.Library.Settings.Instance.ProgramLocalization + "\\Logs\\", DateTime.Now.ToShortDateString() + "*.log", System.IO.SearchOption.TopDirectoryOnly).Length;
            //    if (this.InvokeRequired)
            //        this.BeginInvoke((MethodInvoker)delegate
            //        {
            //            richTextBox_Status.SaveFile(Nizm0.QOT.Library.Settings.Instance.ProgramLocalization + "\\Logs\\" + DateTime.Now.ToShortDateString() + (countfiles <= 0 ? string.Empty : "_" + countfiles) + ".log", RichTextBoxStreamType.PlainText);
            //        });
            //    else
            //        richTextBox_Status.SaveFile(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Logs\\" + DateTime.Now.ToShortDateString() + (countfiles <= 0 ? string.Empty : "_" + countfiles) + ".log", RichTextBoxStreamType.PlainText);
            //}
            //_isSave = true;
        }

        private void AddLogToFile(string text)
        {
            if (!System.IO.Directory.Exists(Nizm0.QOT.Settings.Instance.ProgramLocalization + "\\Logs\\")) System.IO.Directory.CreateDirectory(Nizm0.QOT.Settings.Instance.ProgramLocalization + "\\Logs\\");
            int countfiles = 0;// = System.IO.Directory.GetFiles(Nizm0.QOT.Library.Settings.Instance.ProgramLocalization + "\\Logs\\", DateTime.Now.ToShortDateString() + "*.log", System.IO.SearchOption.TopDirectoryOnly).Length;
            if (string.IsNullOrWhiteSpace(_logfile))
            {
                countfiles = System.IO.Directory.GetFiles(Nizm0.QOT.Settings.Instance.ProgramLocalization + "\\Logs\\", DateTime.Now.ToShortDateString() + "*.log", System.IO.SearchOption.TopDirectoryOnly).Length;
                _logfile = Nizm0.QOT.Settings.Instance.ProgramLocalization + "\\Logs\\" + DateTime.Now.ToShortDateString() + (countfiles <= 0 ? string.Empty : "_" + countfiles) + ".log";
            }
            using (System.IO.StreamWriter strw = new System.IO.StreamWriter(_logfile, true))
            {
                strw.WriteLine(text);
            }
        }

        private void LogNew(string str)
        {
            richTextBox_Status.SelectionColor = Color.Black;
            richTextBox_Status.SelectionBackColor = Color.White;
            richTextBox_Status.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str + Environment.NewLine);
            AddLogToFile(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str);
            _isSave = false;
        }

        private void LogWarning(string str)
        {
            richTextBox_Status.SelectionColor = Color.Red;
            richTextBox_Status.SelectionBackColor = Color.White;
            richTextBox_Status.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str + Environment.NewLine);
            //richTextBox_Status.Select(richTextBox_Status.Text.Length - str.Length, str.Length);
            //richTextBox_Status.Text += Environment.NewLine;
            AddLogToFile(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str);
            _isSave = false;
        }

        private void LogError(string str)
        {
            int pos = richTextBox_Status.TextLength;
            richTextBox_Status.SelectionBackColor = Color.Red;
            richTextBox_Status.SelectionColor = Color.Black;
            richTextBox_Status.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str + Environment.NewLine);
            //richTextBox_Status.Text += Environment.NewLine;
            AddLogToFile(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str);
            _isSave = false;
        }

        private void LogError(string str, Exception ex)
        {
            if (!richTextBox_Status.IsDisposed)
            {
                int pos = richTextBox_Status.TextLength;
                richTextBox_Status.SelectionBackColor = Color.Red;
                richTextBox_Status.SelectionColor = Color.Black;
                richTextBox_Status.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str + " " + ex.ToString() + Environment.NewLine);
            }
            //  richTextBox_Status.SelectionColor = Color.Black;
            AddLogToFile(DateTime.Now.ToString("HH:mm:ss.fff") + " - " + str);
            _isSave = false;
        }

        private void frmStatusWindow_Activated(object sender, EventArgs e)
        {
            base.Activate();
        }

        private void StatusWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Save();
        }

        #region IStatusLog Members

        void Library.Objects.IStatusLog.StatusLog(string str)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
